
using System;
using System.Collections.Generic;
using BTKERP.Model;
using BTKERP.DAL.DataAccess;
using System;
using BTKERP.Model.Web;

namespace BTKERP.DAL
{
	#region RequisitionMasterDAL
	public class RequisitionMasterDAL : DBContext
	{
		public List<RequisitionMaster> GetAll()
        {
            ExSQLCommand cmd = new ExSQLCommand("usp_RequisitionMastersSelectAll");
            return GetEntityList<RequisitionMaster>(cmd);
        }

        public RequisitionMaster GetById(Guid id)
        {
            ExSQLCommand cmd = new ExSQLCommand("usp_RequisitionMasterSelect");
            cmd.AddParameter("@Id", id);
            return GetSingleEntity<RequisitionMaster>(cmd);
        }

		public List<RequisitionMaster> GetByDynamicSQL(string sqlCondition)
        {
            ExSQLCommand cmd = new ExSQLCommand("usp_RequisitionMasterSelectDynamic");
            cmd.AddParameter("@SQL", sqlCondition);
            return GetEntityList<RequisitionMaster>(cmd);
        } 
		
        public bool DeleteById(Guid id)
        {
            ExSQLCommand cmd = new ExSQLCommand("usp_RequisitionMasterDelete");
            cmd.AddParameter("@Id", id);
            return ExecuteCommand(cmd);
        }

        public string AddNewRequisition(Guid fromHouse, Guid toHouse, string code, DateTime requisitionDate, string requestBy, string fullFillBy, string createBy, DateTime deliveryDate)
        {
            ExSQLCommand cmd = new ExSQLCommand("AddRequisitionMaster");
            cmd.AddParameter("@FromWarehouseId", fromHouse);
            cmd.AddParameter("@ToWarehouseId", toHouse);
            cmd.AddParameter("@RequisitionCode", code);
            cmd.AddParameter("@RequisitionDate", requisitionDate);
            cmd.AddParameter("@RequisitionedBy", requestBy);
            cmd.AddParameter("@FulfilledBy", fullFillBy);
            cmd.AddParameter("@CreatedBy", createBy);
            cmd.AddParameter("@deliveryDate", deliveryDate);

            return GetSingleValue(cmd);
        }

        public RequisitionMaster UpdateById(Guid paramId, Guid paramToWarehouseId, string paramRequisitionCode,
                                            DateTime paramRequisitionDate, string requisitionBy, DateTime paramDeliveryDate, decimal paramRequisitionTotal, 
                                             int paramStatus,  string paramUpdatedBy)
        {
            ExSQLCommand cmd = new ExSQLCommand("usp_RequisitionMasterUpdate");
			cmd.AddParameter("@Id", paramId);
			cmd.AddParameter("@ToWarehouseId", paramToWarehouseId);
			cmd.AddParameter("@RequisitionCode", paramRequisitionCode);
			cmd.AddParameter("@RequisitionDate", paramRequisitionDate);
            cmd.AddParameter("@RequisitionedBy", requisitionBy);
			cmd.AddParameter("@DeliveryDate", paramDeliveryDate);
			cmd.AddParameter("@RequisitionTotal", paramRequisitionTotal);
			cmd.AddParameter("@Status", paramStatus);
			cmd.AddParameter("@UpdatedBy", paramUpdatedBy);
            return GetSingleEntity<RequisitionMaster>(cmd);
        }


        public List<RequisitionMaster> SearchMaster(string houseId, string reqCode, string reqDate, string user, int status)
        {
            ExSQLCommand cmd = new ExSQLCommand("SearchReqMaster");
            cmd.AddParameter("@houseId", new Guid(houseId));
            if (!string.IsNullOrEmpty(reqCode)) cmd.AddParameter("@reqCode", reqCode);
            if (!string.IsNullOrEmpty(reqDate)) cmd.AddParameter("@reqDate", DateTime.ParseExact(reqDate, "MM/dd/yyyy", null));
            if (!string.IsNullOrEmpty(user)) cmd.AddParameter("@requestBy", user);
            cmd.AddParameter("@status",status);
            return GetEntityList<RequisitionMaster>(cmd);
        }

        public bool VoidMaster(System.Guid id  ,string name )
        {
            ExSQLCommand cmd = new ExSQLCommand("VoidMaster");
            cmd.AddParameter("@Id", id);
            cmd.AddParameter("@updateBy", name);
            return ExecuteCommand(cmd);
        }

        public bool ChangeStatus(System.Guid id, string name, int status)
        {
            ExSQLCommand cmd = new ExSQLCommand("UpdateMasterStatus");
            cmd.AddParameter("@Id", id);
            cmd.AddParameter("@updateBy", name);
            cmd.AddParameter("@status", status);
            return ExecuteCommand(cmd);
        }

        public string ApproveMaster(System.Guid guid, string name)
        {
            ExSQLCommand cmd = new ExSQLCommand("ApproveMasterStatus");
            cmd.AddParameter("@Id", guid);
            cmd.AddParameter("@name", name);
            return GetSingleValue(cmd);
        }

        public List<ItemCountModel> GetRequisitionCountByStatus()
        {
            ExSQLCommand cmd = new ExSQLCommand("GetRequisitionMasterInfo");
            return GetEntityList<ItemCountModel>(cmd);
        }
    }
	#endregion
}
